{extend name="platform/new_base" /}
{block name="resource"}
<style>
    .label-danger a{padding-top:5px;}
</style>
{/block}
{block name="main"}
                <!-- page -->
                <ul class="nav nav-tabs v-nav-tabs fs-12">
                    <li role="presentation" {if !$type} class="active" {/if}><a href="{:__URL('PLATFORM_MAIN/goods/selfgoodslist')}" class="flex-auto-center">全部<br><span class="J-all"></span></a></li>
                    <li role="presentation" {if condition="$type eq 1"} class="active" {/if}><a href="{:__URL('PLATFORM_MAIN/goods/selfgoodslist','type=1')}" class="flex-auto-center">出售中<br><span class="J-online"></span></a></li>
                    <li role="presentation" {if condition="$type eq 2"} class="active" {/if}><a href="{:__URL('PLATFORM_MAIN/goods/selfgoodslist','type=2')}" class="flex-auto-center">仓库中<br><span class="J-offline"></span></a></li>
                    <li role="presentation" {if condition="$type eq 3"} class="active" {/if}><a href="{:__URL('PLATFORM_MAIN/goods/selfgoodslist','type=3')}" class="flex-auto-center">已售聲<br><span class="J-soldout"></span></a></li>
                    <li role="presentation" {if condition="$type eq 4"} class="active" {/if}><a href="{:__URL('PLATFORM_MAIN/goods/selfgoodslist','type=4')}" class="flex-auto-center">库存预警<br><span class="J-alarm"></span></a></li>
                </ul>
                <div class="mb-10 flex flex-pack-justify">
                    <div class="">
                        <button class="btn btn-default">
                            <label class="checkbox-inline">
                                <input type="checkbox" id="checkAll">全选
                            </label>
                        </button>
                        <button class="btn btn-default online">上架</button>
                        <button class="btn btn-default outline">下架</button>
                        <button class="btn btn-default deleteGood">删除</button>
                    </div>
                    <div class="flex">
                        <div class="select-form-control ">
                            <div class="sotr-selector">
                                <input type="text" class="form-control sotr-selector-input" name="categoryId" placeholder="请选择分类" readonly required>
                                <!-- <span class="sotr-selector-input-text">请选择分类</span> -->
                                <div class="sotr-box">
                                    <div class="list" id="sort1"></div>
                                    <div class="list" id="sort2"></div>
                                    <div class="list" id="sort3"></div>
                                </div>
                            </div>
                        </div>
                        <div class="input-group search-input-group ml-10">
                            <input type="text" class="form-control" placeholder="商品名称"  id="goods_name" value="{$search_info}">
                            <input type="hidden" id="type" value='{$type}'>
                            <span class="input-group-btn">
                                <button class="btn btn-primary search" type="button">搜索</button>
                            </span>
                        </div>
                        <a href="{:__URL('PLATFORM_MAIN/goods/addgoods')}" class="btn btn-primary ml-10"><i class="icon icon-add1"></i> 发布商品</a>
                    </div>
                </div>
                <table class="table v-table table-auto-center" id="selfGoodsList">
                    <thead>
                        <tr>
                            <th></th>
                            <th>商品</th>
                            <th>售价</th>
                            <th>原价</th>
                            <th>库存</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="goods_list">

                    </tbody>
                </table>
                <input type="hidden" id="pageIndex">
                <nav aria-label="Page navigation" class="clearfix">
                    <ul id="page" class="pagination pull-right"></ul>
                </nav>
                <!-- page end  -->
{/block}




{block name="script"}
<script>
require(['util','sotr-selector'],function(util){
        //全选
        util.initPage(LoadingInfo);
    function LoadingInfo(page_index) {
        var start_date = $("#startDate").val();
        var end_date = $("#endDate").val();
//      var state = $("#state").val();
        var goods_name = $("#goods_name").val();
        var category_id_1 = $("#category_id_1").val();
        var category_id_2 = $("#category_id_2").val();
        var category_id_3 = $("#category_id_3").val();
        var type = $("#type").val();
        var selectGoodsLabelId = $("#selectGoodsLabelId").val();
        var supplier_id = $("#supplier_id").val();
        $("#pageIndex").val(page_index);
        $.ajax({
            type: "post",
            url: "{:__URL('PLATFORM_MAIN/goods/selfgoodslist')}",
            data: {
                "page_index": page_index,
                "page_size": $("#showNumber").val(),
                "start_date": start_date,
                "end_date": end_date,
//              "state": state,
                "goods_name": goods_name,
                "category_id_1": category_id_1,
                "category_id_2": category_id_2,
                "category_id_3": category_id_3,
                "selectGoodsLabelId": selectGoodsLabelId,
                "supplier_id": supplier_id,
                "type":type
            },
            success: function (data) {

                var html = '';
                if (data["data"].length > 0) {
                    for (var i = 0; i < data["data"].length; i++) {

                        html += '<tr>';
                        html += '<td>';
                        // html += '<input type="checkbox" value="\' + data["data"][i]["goods_id"] + \'" name="sub" data-state="\' + data["data"][i]["state"] + \'" type="checkbox">';
                        html += '<input value="' + data["data"][i]["goods_id"] + '" name="sub" data-state="' + data["data"][i]["state"] + '" type="checkbox">';

                        html += '</td>';
                        html += '<td>';
                        html += '<div class="media text-left">';
                        html += '<div class="media-left">';
                        html += '<p><img src="' + __IMG(data["data"][i]["pic_cover_micro"]) + '" style="width:60px;height:60px;"></p>';
                        html += '</div>';
                        html += '<div class="media-body max-w-300">';
                        html += '<div class="line-2-ellipsis"><a href="' + __URLS('SHOP_MAIN/goods/goodsinfo&goodsid=' + data["data"][i]["goods_id"]) + '" target="_blank">'+ data["data"][i]["goods_name"] +'</a></div>';
                        html += '<div class="small-muted line-2-ellipsis">'+ data["data"][i]["sku_list"]['0']['sku_name'] +'</div>';
                        html += '</div>';
                        html += '</div>';
                        html += '</td>';
                        html += '<td>';
                        html += '<div class="" id="moreChangePrice' + data["data"][i]["goods_id"] + '">￥'+ data["data"][i]["promotion_price"] +'</div>';
                        html += '</td>';
                        html += '<td>';
                        html += '<div class="">￥'+ data["data"][i]["price"] + '</div>';
                        html += '</td>';
                        html += '<td>';
                        html += data["data"][i]["stock"];
                        html += '</td>';
                        html += '<td>';
                        if (data["data"][i]["state"] == 1) {
                            html += '<a class="label label-default" href="javascript:void(0);" style="background:#5cb85c;">上架</a>';
                        }else if (data["data"][i]["state"] == '0'){
                            html += '<a class="label label-default" href="javascript:void(0);" style="background:#c9302c;">下架</a>';
                        }else{
                            html += '<a class="label label-default" href="javascript:void(0);" >未上架</a>';
                        }
                        html += '</td>';
                        html += '<td>';
                        html += '<a class="text-primary" href="{:__URL("PLATFORM_MAIN/goods/addgoods&goods_id=")}'+data["data"][i]["goods_id"]+'">编辑</a> <a class="text-danger deleteGood" data-id="' + data["data"][i]["goods_id"] + '" href="javascript:deleteGood(' + data["data"][i]["goods_id"] + ')">删除</a>';
                        html += '</td>';
                        html += '</tr>';

                    }
                } else {
                    html += '<tr align="center"><td colspan="8" style="text-align: center;font-weight: normal;color: #999;">暂无符合条件的数据记录</td></tr>';
                }
                $("#goods_list").html(html);
                var totalpage = $("#page_count").val();
                if (totalpage == 1) {
                    changeClass("all");
                }
                $("#pageNumber").append(html);
                deletegood();
            }
        });
    }
        $("#checkAll").on('click',function(){
            $("#selfGoodsList input[type = 'checkbox']").prop("checked", this.checked);
        })

        //搜索
        $('.search').on('click',function(){
            util.initPage(LoadingInfo);
        });

        //删除商品
        function deletegood(){

            $(".deleteGood").on('click',function(){
                var goodsId = [];
                if($(this).data('id')&&!isNaN($(this).data('id'))){
                    goodsId.push($(this).data('id'));
                }
                $("#selfGoodsList input[type = 'checkbox']:checked").each(function(){
                    if (!isNaN($(this).val())) {
                        goodsId.push($(this).val());
                    }
                })
                if(goodsId.length==0){
                    util.message('请选择需要删除的商品','warning');
                    return false;
                }
                util.alert('是否删除此商品？',function(){
                    //util.message('删除的商品id='+goodsId)
                    $.ajax({
                        type: "post",
                        url: "{:__URL('PLATFORM_MAIN/goods/deletegoods')}",
                        data: {"goods_ids": goodsId.toString()},
                        dataType: "json",
                        async : true,
                        success : function(data) {
                            if (data["code"] > 0) {
                                util.message(data["message"],'success',LoadingInfo($('#pageIndex').val()));
                            }else{
                                util.message(data["message"],'error',LoadingInfo($('#pageIndex').val()));
                            }
                        }
                    })

                })

            })
        }

        // 上架

            $(".online").on('click',function(){
                var goodsId = [];
                if($(this).data('id')&&!isNaN($(this).data('id'))){
                    goodsId.push($(this).data('id'));
                }
                $("#selfGoodsList input[type = 'checkbox']:checked").each(function(){
                    if (!isNaN($(this).val())) {
                        goodsId.push($(this).val());
                    }
                })
                if(goodsId.length==0){
                    util.message('请选择需要上架的商品','warning');
                    return false;
                }
                util.alert('是否上架此商品？',function(){
                    //util.message('删除的商品id='+goodsId)
                    $.ajax({
                        type: "post",
                        url: "{:__URL('PLATFORM_MAIN/goods/ModifyGoodsOnline')}",
                        data: {"goods_ids": goodsId.toString()},
                        dataType: "json",
                        async : true,
                        success : function(data) {
                            if (data["code"] > 0) {
                                util.message(data["message"],'success',LoadingInfo($('#pageIndex').val()));
                            }else{
                                util.message(data["message"],'error',LoadingInfo($('#pageIndex').val()));
                            }
                        }
                    })

                })

            })


    // 下架
        $('.outline').on('click',function(){
            var goodsId = [];
            if($(this).data('id')&&!isNaN($(this).data('id'))){
                goodsId.push($(this).data('id'));
            }
            $("#selfGoodsList input[type = 'checkbox']:checked").each(function(){
                if (!isNaN($(this).val())) {
                    goodsId.push($(this).val());
                }
            })
            if(goodsId.length==0){
                util.message('请选择需要下架的商品','warning');
                return false;
            }
            util.alert('是否下架此商品？',function(){
                //util.message('删除的商品id='+goodsId)
                $.ajax({
                    type: "post",
                    url: "{:__URL('PLATFORM_MAIN/goods/ModifyGoodsOutline')}",
                    data: {"goods_ids": goodsId.toString()},
                    dataType: "json",
                    async : true,
                    success : function(data) {
                        if (data["code"] > 0) {
                            util.message(data["message"],'success',LoadingInfo($('#pageIndex').val()));
                        }else{
                            util.message(data["message"],'error',LoadingInfo($('#pageIndex').val()));
                        }
                    }
                })

            })

        })

    getgoodscount();
    //获取商品各种状态的数量
    function getgoodscount(){
        $.ajax({
            type: "post",
            url: "{:__URL('PLATFORM_MAIN/goods/getgoodscount')}",
            success: function (data) {
                $('.J-all').html('('+data.all+')');
                $('.J-online').html('('+data.sale+')');
                $('.J-offline').html('('+data.shelf+')');
                $('.J-soldout').html('('+data.soldout+')');
                $('.J-alarm').html('('+data.alarm+')');
            }
        })
    }

})
    
</script>
{/block}
